package com.anyou.web.controller.common;


import com.anyou.common.core.controller.BaseController;
import com.anyou.common.core.domain.AjaxResult;
import com.anyou.common.oss.TencentOssUtil;
import com.anyou.use.vo.FileInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传
 *
 * @author clx
 * @date 2021/12/31 16:37
 */
@Api(tags = "文件上传")
@Slf4j
@RestController
@RequestMapping("/common/upload")
public class UploadController extends BaseController {

    /**
     * 文件上传请求
     */
    @ApiOperation(value = "文件上传API")
    @PostMapping("/uploadFiles")
    @ApiImplicitParam(name = "file", value = "文件流对象", required = true, dataType = "MultipartFile", allowMultiple = true)
    public AjaxResult uploadFile(@RequestParam(value = "file", required = true) MultipartFile file) {
        String url = TencentOssUtil.upload(file);
        FileInfo fileInfo = new FileInfo();
        String originalFilename = file.getOriginalFilename();
        if(originalFilename.indexOf(".")==-1){
            originalFilename=originalFilename+".jpg";
        }
        fileInfo.setName(originalFilename.substring(0,originalFilename.lastIndexOf(".")));
        fileInfo.setUrl(url);
        return AjaxResult.success(fileInfo);
    }
}
